package com.kkb.robotcustomerservice.controller;


import com.kkb.robotcustomerservice.config.BM25SimilarityConfig;
import com.kkb.robotcustomerservice.entities.Chat;
import com.kkb.robotcustomerservice.service.GeneralAnswerService;
import com.kkb.robotcustomerservice.util.JsonResult;
import com.kkb.robotcustomerservice.entities.Questions;
import com.kkb.robotcustomerservice.service.ChatService;
import com.kkb.robotcustomerservice.service.QuestionsService;
import com.kkb.robotcustomerservice.util.SendTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


@RestController
@CrossOrigin
public class SendMessageController {

    @Autowired
    private QuestionsService questionsService;
    @Autowired
    private ChatService chatService;
    @Autowired
    GeneralAnswerService generalAnswerService;

    @Autowired
    private Questions questions;

    @Autowired
    BM25SimilarityConfig bm25SimilarityConfig;

    @RequestMapping(value = "/chat/sendmessage/{id}/{type}/{question}/{sender}",method = RequestMethod.POST)
    public JsonResult sendMessage(@PathVariable("id")Integer id,
                                 @PathVariable("type")Integer type,
                                 @PathVariable("question")String question,
                                 @PathVariable("sender")Integer sender) {

        SendTime time = new SendTime();
        Chat chat = new Chat();
        JsonResult<Questions> result = new JsonResult<>();

        //前端写死type=1跳过
        if (type==0) {
            Questions byId = questionsService.findById(id);
            String questionName = byId.getQuestionName();
            chat.setChatInfo(questionName);
            chat.setUserId(1);
            chat.setSender(sender);
            chat.setSendTime(time.sendTime());
            chatService.add(chat);

            String answer1 = byId.getAnswer();
            questions.setAnswer(answer1);
            result.setData(byId);
            result.setCode(0);
            if (answer1.isEmpty()){
                result.setMsg("数据库找不到");
            }else{
                result.setMsg("成功");
            }
            result.setSuccess(true);

            Chat chat1 = new Chat();
            chat1.setChatInfo(answer1);
            chat1.setSender(0);
            chat.setUserId(1);
            chat1.setSendTime(time.sendTime());
            chatService.add(chat1);
            return result;

            //直接跳到这里
        }else if (type==1){
            chat.setChatInfo(question);
            chat.setUserId(1);
            chat.setSender(sender);
            chat.setSendTime(time.sendTime());
            chatService.add(chat);

            //得到答案，先计算所有客户问题数据寻找答案，如果找不到，再去发送通用问题答案
            String answer = "";
            //先写死，意思为查不到答案
            if (answer.equals("")){
                Map map = bm25SimilarityConfig.getConfidence(question);
                double confidence = (double) map.get("confidence");
                if (confidence>0.6){
                    Integer qid = (Integer) map.get("qid");
                    //查询答案
                    try {
                        answer = generalAnswerService.getByQid(qid).getContent();
                    }catch (Exception e){
                        answer = "不好意思，我还没有学会这个问题，您可以换个问题问我";
                    }
                }else {
                    //数据库和通用库都查不到
                    answer = "不好意思，我还没有学会这个问题，您可以换个问题问我";
                }
            }
            questions.setAnswer(answer);
            result.setData(questions);
            result.setCode(0);
            if (answer.isEmpty()){
                result.setMsg("数据库找不到");
            }
            result.setSuccess(true);

            Chat chat1 = new Chat();
            chat1.setChatInfo(answer);
            chat1.setSender(0);
            chat.setUserId(1);
            chat.setSendTime(time.sendTime());
            chatService.add(chat1);
            return result;
        }else {
            return result;
        }
    }
}
